import axios from "../../utils/axios"


// pages/mypost/mypost.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar:["我的帖子","收到评论","我的评论"],
    currentindex:0,
    dynamics:[],
    user:{},
    commemts:[],
    mycomments:[],
  },
  async getdynamic(){
    var result=await axios.get("/dynamics?personal=true")
    console.log(result)
    var dynamicslist=[]
    for (let i = 0; i < result.content.length; i++) {
      const item = result.content[i];
      var dynamics={id:item[0],
        content:item[1],
        anonymous:item[4],
        provided:item[5],
        contact:item[9],
        commentnumber:item[10],
        lovenumber:item[12],
        islove:item[11],
      userid:item[8]}
      var tags=item[3].split(",")
      dynamics.tags=tags
    dynamics.src=item[2].split(",")
    var datetime=new Date(item[7])
    var datetimestr=""
    var period=new Date().getTime()-datetime.getTime()
    if(period<60*60*1000){
      var t=period/(60*1000)
      datetimestr+=t.toFixed(0)+"分钟"
    }else{
      if(period<24*60*60*1000){
        var t=period/(60*60*1000)
        datetimestr+=t.toFixed(0)+"小时"
      }else{
        if(period<30*24*60*60*1000){
          var t=period/(24*60*60*1000)
          datetimestr+=t.toFixed(0)+"天"
        }else{
          if(period<12*30*24*60*60*1000){
            var t=period/(30*24*60*60*1000)
            datetimestr+=t.toFixed(0)+"月"
          }else{
            var t=period/(12*30*34*60*1000)
            datetimestr+=t.toFixed(0)+"年"
          }
        }
      }
    }
    datetimestr+="前"
    dynamics.datetime=datetimestr
    dynamicslist.push(dynamics)
    }
    var user=getApp().globalData.user
    this.setData({
      dynamics:dynamicslist,
      user:user
    })
    console.log(user)
    console.log(this.data.dynamics)
  },
  async getcomment(){
    var token=getApp().globalData.accessToken
    var result=await axios.get('/dynamics/comment/receive')
    console.log(result)
    var comments=[]
    for (let i = 0; i < result.length; i++) {
      const item = result[i];
      var comment={}
      comment.comment=item.content
      var datetime=new Date(item.updatedAt)
      var now=new Date()
      var datetimestr=""
      if(datetime.getMonth()-now.getMonth()>0){
         datetimestr=datetime.getMonth()-now.getMonth()+"月前"
      }else{
        if(datetime.getMonth()-now.getMonth()==0&&datetime.getDate()-now.getDate()>0){
           datetimestr=datetime.getDate()-now.getDate()+"日前"
        }else{
          if(datetime.getMonth()-now.getMonth()==0&&datetime.getDate()-now.getDate()==0&&datetime.getHours()-now.getHours()>0){
            datetimestr=datetime.getHours()-now.getHours()+"小时前"
          }else{
            if(datetime.getMonth()-now.getMonth()==0&&datetime.getDate()-now.getDate()==0&&datetime.getHours()-now.getHours()==0&&datetime.getMinutes()-now.getMinutes()){
              datetimestr=datetime.getMinutes()-now.getMinutes()+"分钟前"
            }else{
              datetimestr="一分钟前"
            }
          }
        }
      }
        comment.time=datetimestr
        comments.push(comment)
    }
    this.setData({
      comments:comments
    })
  },
  async getmycomment(){
    var result=await axios.get('/dynamics/comment/myself')
    console.log(result)
    var comments=[]
    for (let i = 0; i < result.length; i++) {
      const item = result[i];
      var comment={}
      comment.comment=item.content
      var datetime=new Date(item.updatedAt)
      var now=new Date()
      var datetimestr=""
      if(datetime.getMonth()-now.getMonth()>0){
         datetimestr=datetime.getMonth()-now.getMonth()+"月前"
      }else{
        if(datetime.getMonth()-now.getMonth()==0&&datetime.getDate()-now.getDate()>0){
           datetimestr=datetime.getDate()-now.getDate()+"日前"
        }else{
          if(datetime.getMonth()-now.getMonth()==0&&datetime.getDate()-now.getDate()==0&&datetime.getHours()-now.getHours()>0){
            datetimestr=datetime.getHours()-now.getHours()+"小时前"
          }else{
            if(datetime.getMonth()-now.getMonth()==0&&datetime.getDate()-now.getDate()==0&&datetime.getHours()-now.getHours()==0&&datetime.getMinutes()-now.getMinutes()){
              datetimestr=datetime.getMinutes()-now.getMinutes()+"分钟前"
            }else{
              datetimestr="一分钟前"
            }
          }
        }
      }
        comment.time=datetimestr
        comments.push(comment)
    }
    this.setData({
      mycomments:comments
    })
  },
  async changeindex(e){
    var index=e.currentTarget.dataset.index
    this.setData({
      currentindex:index
    })
    if(this.data.currentindex==0){
      this.getdynamic()
    }else{
      if(this.data.currentindex==1){
       this.getcomment()
      }else{
        this.getmycomment()
      }
    }
  },
  getphone(e){
    var index=e.currentTarget.dataset.index
    wx.setClipboardData({
      data: this.data.dynamics[index].contact,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  changelove:function(e){
    var index=e.currentTarget.dataset.index
    var love=e.currentTarget.dataset.love
    console.log("index",index)
    console.log("love",love)
    this.setData({
      ['dynamics['+index+'].islove']:love
    })
    console.log(this.data.dynamics)
    if(love){
      this.setData({
      ['dynamics['+index+'].lovenumber']:this.data.dynamics[index].lovenumber+1
      })
    }else{
      this.setData({
        ['dynamics['+index+'].lovenumber']:this.data.dynamics[index].lovenumber-1
        })
    }
    var id=this.data.dynamics[index].id
    var token=getApp(). globalData.accessToken
    console.log(id)
    wx.request({
      url:"http://120.79.70.221:8080/dynamics/likes?id="+id+"&like="+love,
      method:'PATCH',
      header:{
          'content-type': 'application/json',
          'Authorization': 'Bearer '+token
      },
      success:function(res){
        console.log(res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    this.getdynamic()
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})